package com.lqian.demo01;

import com.lqian.utils.SleepHelper;

public class Interrupt_and_wait {

    //作为锁
    private static final Object o = new Object();

    public static void main(String[] args) {
        Thread t = new Thread(() -> {
            // 当你调用wait这个方法时，它本身时抛出InterruptException这个异常的
            // wait()必须捕获异常
            // 如果你在当前线程调用了 t.interrupt();
            // 只要你catch这个异常 默认会帮你把标志位复位
            // 抛出异常怎么处理，掌握在你手中
           synchronized (o){
               try {
                   o.wait();
               } catch (InterruptedException e) {
                   System.out.println("Thread is interrupted!!");
                   System.out.println(Thread.currentThread().isInterrupted());
               }
           }
        });

        t.start();
        SleepHelper.sleepSeconds(3);
        t.interrupt();
    }
}
